<div class="box_comment">
<dl class="massage_comment">
    <a name="#comment"></a>
    <dt><em >{:L('total_of')}<span id="total_count">0</span>{:L('twig')}</em>{:L('comment')}</dt>
    <dd><textarea rows="" cols="" name="comment_content" style="width: 98%;" id="comment_{$appid}" class="message_text text"></textarea></dd>
    <dd> 
        <input type="submit" onclick="doAddComment()" value="{:L('confirm')}" id="comment_commit" class="N_but right">
        <span><a target_set="comment_{$appid}" onclick="ui.emotions(this)" href="javascript:void(0)" class="faceicon1"></a></span>
        <span><label><input type="checkbox" value="1" id="with_new_weibo">{:L('send_a_microblog_sametime')}</label></span>
    </dd>
 </dl>   
</div>
<div class="feedBox">
    <ul class="feed_list">
    </ul>
    <div class="page" id="comment_page"></div>
    <input type="hidden" name="to_id" id="reply_{$appid}" />
</div>
<script>
$(document).ready(function(){
	getCommentList();
	$("#comment_page a").live('click',function(){
		getCommentList( $(this).attr('href') );
		return false;
	});
	
    $("#comment_{$appid}").keyup(function(event){
		if(event.keyCode==32 || event.keyCode==8 || event.keyCode==13){
			checkCommentLength(this,<php> echo $ts['site']['length']; </php>);
		}
	}).keypress(function(){
		checkCommentLength(this,<php> echo $ts['site']['length']; </php>);
	}).blur(function(){
		checkCommentLength(this,<php> echo $ts['site']['length']; </php>);
	}).keydown(function(){
		checkCommentLength(this,<php> echo $ts['site']['length']; </php>);
	}).keyup(function(){
		checkCommentLength(this,<php> echo $ts['site']['length']; </php>);
	});

    shortcut('ctrl+return',	function(){doAddComment();},{'target':'comment_<?php echo $appid;?>'});
});

function checkCommentLength(obj,num){
  	 	var $obj = $(obj);
		var str  = $obj.val();  
		var len  = getLength(str);
		if( len > num ){
			$obj.val(subStr(str, num));
		}
}

//获取评论列表
function getCommentList(url){
	var type = "{$type}";
	var appid = "{$appid}";
	url = url || "{:U('home/Comment/getComment')}";
	$(".feed_list").html('<li class="loading">loading....</li>');
	$.get(url,{type:type,appid:appid},function(txt){
		txt = $.parseJSON(txt);
		if( txt.data ){
			var html = '';
			$.each(txt.data,function(i,n){
				html+=_parseTemplate(n);
			});
			$("#total_count").html( txt.count );
			$('.feed_list').html(html);
			$('#comment_page').html(txt.html);
		}else{
			$(".feed_list").html('<li class="lineD_btm" rel="nocomment">'+"{:L('no_comment_yet')}"+'</li>');
		}
	});
}

function _parseTemplate(n){
	var html = '';
	var delHtml = (n.del_state==1)?'<a onclick="ui.confirm(this,\''+"{:L('sure_del_comment')}"+'\')" callback="doDelComment('+n.id+')" href="javascript:void(0)">{:L('del')}</a> | ':'';
	return html+='<li class="lineD_btm" id="global_comment_li_'+n.id+'">'
		+ '<div class="userPic">'+ n.uavatar +'</div>'
		+ '<div style="margin-left: 65px;" class="feed_c">'
		+ 	'<div class="msgCnt" style="padding-bottom: 0pt; font-size:12px;">'+n.uspace+' '+n.userGroupIcon+' <em>('+n.ctime+')</em></div>'
		+ 	'<div class="feed_c_btm">'
		+   n.comment
		+   '</div>'
		+  '<div class="alR">'+delHtml+'<a onclick="doreply(\''+n.uname+'\','+n.id+')" href="javascript:void(0)">'+"{:L('reply')}"+'</a></div>'
		+ '</div>'
		+ '</li>';
}

//回复评论
function doreply(uname,id){
	$("#comment_{$appid}").val( "{:L('reply_at')}"+uname+' : ' ).focus();
	$("#reply_{$appid}").val(id);
	
	var textArea = document.getElementById("comment_{$appid}");
	var strlength = textArea.value.length;
	if (document.selection) { //IE
		 var rng = textArea.createTextRange();
		 rng.collapse(true);
		 rng.moveStart("character",strlength)
	}else if (textArea.selectionStart || (textArea.selectionStart == '0')) { // Mozilla/Netscape…
        textArea.selectionStart = strlength;
        textArea.selectionEnd = strlength;
    }	
}

//添加评论
function doAddComment(){
	var content = $("#comment_{$appid}").val();
	if( $.trim(content) =='' ){
		ui.error("{:L('comment_nonull')}");
		return false;
	}
	var with_new_weibo = $('#with_new_weibo').attr('checked') ? '1' : '0';
	var type = "{$type}";
	var appid = "{$appid}";
	var to_id  = $("#reply_{$appid}").val();
	var author_uid = "{$author_uid}";
	var title = "{$title}";
	var url = "{$url}";
	var table = "{$table}";
	var id_field = "{$id_field}";
	var comment_count_field = "{$comment_count_field}";
	var $comment_commit = $("#comment_commit");
	$comment_commit.val("{:L('commenting')}");
	$comment_commit.attr('disabled','true');
	$.post(U('home/Comment/doAddComment'),{comment:content,with_new_weibo:with_new_weibo,type:type,appid:appid,to_id:to_id,author_uid:author_uid,title:title,url:url,table:table,id_field:id_field,comment_count_field:comment_count_field},function(txt){
		txt = $.parseJSON(txt);
		var html = _parseTemplate(txt.data);
		if( $('.feed_list').find('li[rel="nocomment"]').html()==null ){
			$('.feed_list').prepend(html);
		}else{
			$('.feed_list').html(html);
		}
		
		$("#comment_{$appid}").val('');
		$("#total_count").html( parseInt($("#total_count").html())+1 );
		$("#comment_commit").val("{:L('confirm')}");
		ui.success("{:L('comment_success')}");
		$comment_commit.removeAttr('disabled');	
	});
}

//删除评论
function doDelComment(id){
	var type = "{$type}";
	$.post(U('home/Comment/doDelete'),{type:type,id:id},function(txt){
		if(txt=='1'){
			$("#global_comment_li_"+id).slideUp("fast",function(){
				$("#total_count").html( parseInt($("#total_count").html())-1 );
			    $(this).remove();
			    if( $('.feed_list li').length==0 ){
			    	$(".feed_list").html('<li class="lineD_btm" rel="nocomment">暂无相关评论</li>');
			    }
		 	}); 
		}else{
			ui.error("{:L('del_error')}");
		}
	})
}
</script>